package ch.abcm.remsmobile.REMSMobile.multicastreceiver;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import ch.abcm.remsmobile.REMSMobile.externnotifier.ExternNotifierController;
import ch.bfh.abcm.rems.gateway.GatewayMessage;
import ch.bfh.abcm.rems.interfaces.REMSMessageReceiverAdapter;

public class REMSMulticastReceiver extends REMSMessageReceiverAdapter {

	private static final String TAG = "REMSMulticastReceiver";

	private Context context;
	
	public REMSMulticastReceiver(Context context) {
		this.context = context;
	}
	
	@Override
	public void receivedGateWayMessage(GatewayMessage gatewayMessage) {
		Log.i(TAG, "Received a GatwayMessage");
		switch (gatewayMessage.getMessageType()) {
		case PERIODIC_NOTIFICATION:
			Log.d(TAG, "Its a PERIODIC_NOTIFICATION");
			sendSimpleExternNotification(context, "PERIODIC_NOTIFICATION: "+gatewayMessage.getMessage());
			break;
		case MEASUREMENT_DIVERGENCE:
			Log.d(TAG, "Its a MEASUREMENT_DIVERGENCE");
			sendSimpleExternNotification(context, "MEASUREMENT_DIVERGENCE: "+gatewayMessage.getMessage());
			break;
		case SYSTEM_ERROR:
			Log.d(TAG, "Its a SYSTEM_ERROR");
			sendSimpleExternNotification(context, "SYSTEM_ERROR: "+gatewayMessage.getMessage());
			break;
		default:
			Log.e(TAG, "Received a Gatwaymessage with a unknown Type: " + gatewayMessage.getMessageType());
			break;
		}
	}

	private void sendSimpleExternNotification(Context context, String message) {
		Intent sentPowerNotification = ExternNotifierController.getSimpleMessageIntent(message);
		context.sendBroadcast(sentPowerNotification);
	}
}
// private void insertIt() {
// databaseOpenHelper = OpenHelperManager.getHelper(this,
// DatabaseOpenHelper.class);
// RuntimeExceptionDao<Treshhold, Integer> dao =
// databaseOpenHelper.getTreshholdruntimeExDao();
//
// Treshhold treshhold = new Treshhold(GateWayMessageType.SYSTEM_ERROR, 300,
// 400);
// dao.create(treshhold);
// OpenHelperManager.releaseHelper();
// }
//
// private void listIt(){
// databaseOpenHelper = OpenHelperManager.getHelper(this,
// DatabaseOpenHelper.class);
// RuntimeExceptionDao<Treshhold, Integer> dao =
// databaseOpenHelper.getTreshholdruntimeExDao();
// databaseOpenHelper = OpenHelperManager.getHelper(this,
// DatabaseOpenHelper.class);
// List<Treshhold> treshholds = dao.queryForAll();
// for (Treshhold treshhold2 : treshholds) {
// Toast.makeText(getApplicationContext(), treshhold2.toString(),
// Toast.LENGTH_SHORT).show();
// }
// OpenHelperManager.releaseHelper();
// }